package com.app.controller;

import com.app.common.Result;
import com.app.model.MessageBoxModel;
import com.app.model.UserModel;
import com.app.service.MessageBoxService;
import com.app.service.UserService;
import com.app.utils.TokenUtils;
import com.app.vo.MessageBoxVo;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 消息盒子表
 *
 * @author 16693103420
 * @since 2024-02-04
 */
@RestController
@RequestMapping("/messageBox")
public class MessageBoxController {
    @Autowired
    private MessageBoxService messageBoxService;
    @Autowired
    private UserService userService;


    // 查询用户转账消息
    @GetMapping("/getUserMessageBox")
    public Result getUserMessageBox(@RequestParam(defaultValue = "0") Integer page,
                                    @RequestParam(defaultValue = "10") Integer limit, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        Integer userId = userModel.getId();
        MessageBoxVo userMessageBox = messageBoxService.getUserMessageBox(page, limit, userId);
        return Result.success(userMessageBox);
    }

//    //用户转账操作
//    @PostMapping("/transferAccounts")
//    public Result transferAccounts(@RequestBody MessageBoxModel messageBoxModel, HttpServletRequest request) {
//        String token = request.getHeader("token");
//        if (token == null) {
//            return Result.error(405, "token不存在");
//        }
//        String openId = TokenUtils.verify(token);
//        UserModel user = userService.getUserByOpenid(openId);
//        Integer userId = user.getId();
//        String invitationCode = messageBoxModel.getInvitationCode();
//        Double voucher = messageBoxModel.getVoucher();
//        QueryWrapper<UserModel> quiltUserIdQW = new QueryWrapper<>();
//        quiltUserIdQW.eq("invitation_code", invitationCode);
//        UserModel quiltUserModel = userService.getOne(quiltUserIdQW);
//        if (quiltUserModel.getVoucher() < voucher) {
//            return Result.error(201, "转账失败,账户余额不足");
//        }
//        Integer quiltUserId = quiltUserModel.getId();
//        messageBoxService.transferAccounts(userId, quiltUserId, voucher);
//        return Result.success("转账成功");
//    }

    /**
     * 获取未读消息的数量
     */
    @GetMapping("/notReadMsgNum")
    public Result notReadMsgNum(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel user = userService.getUserByOpenid(openId);
        Integer userId = user.getId();
        Wrapper<MessageBoxModel> wq = new QueryWrapper<MessageBoxModel>()
                .eq("user_id", userId)
                .eq("if_read", false);
        int count = messageBoxService.count(wq);
        return Result.success(count);
    }

}

